package com.gtw.utils;

import com.alibaba.fastjson.JSON;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbUser;
import com.gtw.modules.entity.TbWalletPayLog;
import com.gtw.wxfullpay.controller.TransferController;
import com.gtw.wxfullpay.model.JsonResult;
import com.gtw.wxfullpay.model.ResponseData;
import com.gtw.wxfullpay.utils.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wuye on 2018/4/2.
 */
@Service
public class TransferPay {

    @Autowired
    private TbUserService tbUserService;

    private static final Logger LOG = Logger.getLogger(TransferController.class);

    private static final String TRANSFERS_PAY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; // 企业付款

    private static final String TRANSFERS_PAY_QUERY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo"; // 企业付款查询

    private static final String APP_ID = "wx2ae76aac1e31617e";    //ConfigUtil.getProperty("wx.appid");

    private static final String MCH_ID = "1513550111";    //ConfigUtil.getProperty("wx.mchid");

    private static final String API_SECRET = "d2fb2c40bdb842d04da11f7c82fc1fd8";    //ConfigUtil.getProperty("wx.api.secret");


    /**
     * 企业向个人支付转账
     * @param request
     * @param response
     */
    public int transferPay(HttpServletRequest request, HttpServletResponse response, TbWalletPayLog tbWalletPayLog) {
        LOG.info("[/transfer/pay]");
        //业务判断 openid是否有收款资格

        Map<String, String> restmap = null;
        try {
            Map<String, String> parm = new HashMap<String, String>();
            parm.put("mch_appid", APP_ID); //公众账号appid
            parm.put("mchid", MCH_ID); //商户号
            parm.put("nonce_str", PayUtil.getNonceStr()); //随机字符串
            parm.put("partner_trade_no", PayUtil.getTransferNo()); //商户订单号
            TbUser tbUser =  tbUserService.queryByUid(tbWalletPayLog.getUid());
            parm.put("openid", tbUser.getWxopenid()); //用户openid
            parm.put("check_name", "NO_CHECK"); //校验用户姓名选项 OPTION_CHECK
//            parm.put("re_user_name", tbUser.getNickname()); //check_name设置为FORCE_CHECK或OPTION_CHECK，则必填
            int total = (tbWalletPayLog.getTotalfee()/100);
            String money = String.valueOf(total);
            parm.put("amount", money); //转账金额
            parm.put("desc", "转账到个人"); //企业付款描述信息
            parm.put("spbill_create_ip", PayUtil.getLocalIp(request)); //Ip地址
            parm.put("sign", PayUtil.getSign(parm, API_SECRET));
//            parm.put("sign",com.lijing.wechatpay.impl.PayImpl.paySign(parm, API_SECRET));
            String str = PayUtil.getSign(parm, API_SECRET);
            System.out.println(str);
            System.out.println(parm);

            String restxml = HttpUtils.posts(TRANSFERS_PAY, XmlUtil.xmlFormat(parm, false));
            restmap = XmlUtil.xmlParse(restxml);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))) {
            LOG.info("转账成功：" + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
            Map<String, String> transferMap = new HashMap<>();
            transferMap.put("partner_trade_no", restmap.get("partner_trade_no"));//商户转账订单号
            transferMap.put("payment_no", restmap.get("payment_no")); //微信订单号
            transferMap.put("payment_time", restmap.get("payment_time")); //微信支付成功时间
//            WebUtil.response(response,
//                    WebUtil.packJsonp(callback,
//                            JSON.toJSONString(new JsonResult(1, "转账成功", new ResponseData(null, transferMap)),
//                                    SerializerFeatureUtil.FEATURES)));
            return 1;
        }else {
            if (CollectionUtil.isNotEmpty(restmap)) {
                LOG.info("转账失败：" + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
            }
//            WebUtil.response(response, WebUtil.packJsonp(callback, JSON
//                    .toJSONString(new JsonResult(-1, "转账失败", new ResponseData()), SerializerFeatureUtil.FEATURES)));
            return 0;
        }
    }



    /**
     * 企业向个人转账查询
     * @param request
     * @param response
     * @param tradeno 商户转账订单号
     * @param callback
     */
    public void orderPayQuery(HttpServletRequest request, HttpServletResponse response, String tradeno,
                              String callback) {
        LOG.info("[/transfer/pay/query]");
        if (StringUtil.isEmpty(tradeno)) {
            WebUtil.response(response, WebUtil.packJsonp(callback, JSON
                    .toJSONString(new JsonResult(-1, "转账订单号不能为空", new ResponseData()), SerializerFeatureUtil.FEATURES)));
        }

        Map<String, String> restmap = null;
        try {
            Map<String, String> parm = new HashMap<String, String>();
            parm.put("appid", APP_ID);
            parm.put("mch_id", MCH_ID);
            parm.put("partner_trade_no", tradeno);
            parm.put("nonce_str", PayUtil.getNonceStr());
            parm.put("sign", PayUtil.getSign(parm, API_SECRET));

            String restxml = HttpUtils.posts(TRANSFERS_PAY_QUERY, XmlUtil.xmlFormat(parm, true));
            restmap = XmlUtil.xmlParse(restxml);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        if (CollectionUtil.isNotEmpty(restmap) && "SUCCESS".equals(restmap.get("result_code"))) {
            // 订单查询成功 处理业务逻辑
            LOG.info("订单查询：订单" + restmap.get("partner_trade_no") + "支付成功");
            Map<String, String> transferMap = new HashMap<>();
            transferMap.put("partner_trade_no", restmap.get("partner_trade_no"));//商户转账订单号
            transferMap.put("openid", restmap.get("openid")); //收款微信号
            transferMap.put("payment_amount", restmap.get("payment_amount")); //转账金额
            transferMap.put("transfer_time", restmap.get("transfer_time")); //转账时间
            transferMap.put("desc", restmap.get("desc")); //转账描述
            WebUtil.response(response, WebUtil.packJsonp(callback, JSON
                    .toJSONString(new JsonResult(1, "订单转账成功", new ResponseData(null, transferMap)), SerializerFeatureUtil.FEATURES)));
        }else {
            if (CollectionUtil.isNotEmpty(restmap)) {
                LOG.info("订单转账失败：" + restmap.get("err_code") + ":" + restmap.get("err_code_des"));
            }
            WebUtil.response(response, WebUtil.packJsonp(callback, JSON
                    .toJSONString(new JsonResult(-1, "订单转账失败", new ResponseData()), SerializerFeatureUtil.FEATURES)));
        }
    }


}
